<template>
  <div class="login">
    <div class="header">
      <h3 class="title">go for IT 华医管理系统</h3>
    </div>
    <div class="login_box">
      <h3>欢迎登录</h3>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user"
            type="username"
            placeholder="请输入用户名"
            v-model="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            type="password"
            placeholder="请输入密码"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button class="submit" type="primary" @click="submitForm()"
            >登录</el-button
          >
          <el-button class="reset" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios"

export default {
  data() {
    var validateName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (value.length < 6) {
        callback(new Error("密码至少6位"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      // 检测规则
      rules: {
        username: [{ validator: validateName, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
      },
    };
  },

  // 方法
  methods: {
    // 提交
    submitForm() {
      axios.post("http://47.108.201.43:3000/api/login", {
        username: this._data.ruleForm.username,
        password: this._data.ruleForm.password,
      }).then((res)=>{
        // console.log(res);
        if(res.data.code == "200") {
          this.$message({
            message: '登录成功',
            type: 'success'
          });
          // 跳转传数据
          this.$router.replace({
            name: 'Datas',
            params: {
              userInfo: res.data.reslut
            }
          });
        } else {
          this.$message({
            message: '登录失败：' + res.data.error,
            type: 'info'
          });
        }
      });
    },

    // 重置表单
    resetForm(formName){
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.login {
  width: 100vw;
  height: 100vh;
  background-image: url("http://47.108.201.43:4000/img/login_bg.2fd13619.png");
  background-size: 100% 100%;
  position: relative;
  font-family: Microsoft YaHei light,微软雅黑,Arial,Tahoma,simhei,Helvetica,sans-serif;
}
.header {
  height: 80px;
  line-height: 80px;
  background-color: hsla(0,0%,100%,.2);
  text-align: center;
}
.title {
  letter-spacing: 1px;
  font-size: 32px;
  margin-left: 20px;
  font-weight: 700;
  color: #fffffd;
}
.login_box {
  position: absolute;
  width: 400px;
  background-color: #fff;
  padding: 20px 40px;
  box-sizing: border-box;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
}
.login_box>h3{
  text-align: center;
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 20px;
}
.btns{
  height: 30px;
}
.submit{
  position: absolute;
  width: 200px;
}
.reset{
  position: absolute;
  width: 110px;
  right: 0;
}
</style>